package cn.iocoder.yudao.module.pplan.controller.admin.recipemeal;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.recipemeal.vo.RecipeMealPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.recipemeal.vo.RecipeMealRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.recipemeal.vo.RecipeMealSaveReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.recipemeal.RecipeMealDO;
import cn.iocoder.yudao.module.pplan.service.recipemeal.RecipeMealService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 生产计划-餐谱菜品数据管理")
@RestController
@RequestMapping("/pplan/recipe-meal")
@Validated
public class RecipeMealController {

    @Resource
    private RecipeMealService recipeMealService;

    @PostMapping("/create")
    @Operation(summary = "创建生产计划-餐谱菜品数据管理")
    @PreAuthorize("@ss.hasPermission('pplan:recipe-meal:create')")
    public CommonResult<Long> createRecipeMeal(@Valid @RequestBody RecipeMealSaveReqVO createReqVO) {
        return success(recipeMealService.createRecipeMeal(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产计划-餐谱菜品数据管理")
    @PreAuthorize("@ss.hasPermission('pplan:recipe-meal:update')")
    public CommonResult<Boolean> updateRecipeMeal(@Valid @RequestBody RecipeMealSaveReqVO updateReqVO) {
        recipeMealService.updateRecipeMeal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产计划-餐谱菜品数据管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pplan:recipe-meal:delete')")
    public CommonResult<Boolean> deleteRecipeMeal(@RequestParam("id") Long id) {
        recipeMealService.deleteRecipeMeal(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除生产计划-餐谱菜品数据管理")
                @PreAuthorize("@ss.hasPermission('pplan:recipe-meal:delete')")
    public CommonResult<Boolean> deleteRecipeMealList(@RequestParam("ids") List<Long> ids) {
        recipeMealService.deleteRecipeMealListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产计划-餐谱菜品数据管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pplan:recipe-meal:query')")
    public CommonResult<RecipeMealRespVO> getRecipeMeal(@RequestParam("id") Long id) {
        RecipeMealRespVO recipeMealRespVO = recipeMealService.getRecipeMeal(id);
        return success(recipeMealRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产计划-餐谱菜品数据管理分页")
    @PreAuthorize("@ss.hasPermission('pplan:recipe-meal:query')")
    public CommonResult<PageResult<RecipeMealRespVO>> getRecipeMealPage(@Valid RecipeMealPageReqVO pageReqVO) {
        PageResult<RecipeMealDO> pageResult = recipeMealService.getRecipeMealPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecipeMealRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产计划-餐谱菜品数据管理 Excel")
    @PreAuthorize("@ss.hasPermission('pplan:recipe-meal:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecipeMealExcel(@Valid RecipeMealPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecipeMealDO> list = recipeMealService.getRecipeMealPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产计划-餐谱菜品数据管理.xls", "数据", RecipeMealRespVO.class,
                        BeanUtils.toBean(list, RecipeMealRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得菜品精简列表", description = "主要用于前端的下拉选项")
    public CommonResult<List<RecipeMealRespVO>> getSimpleTablewareist(@RequestParam("name") String name) {
        RecipeMealPageReqVO reqVO = new RecipeMealPageReqVO();
        reqVO.setMealName(name);
        reqVO.setPageSize(PAGE_SIZE_NONE); // 不分页
        List<RecipeMealDO> list = recipeMealService.getRecipeMealPage(reqVO).getList();
        // 拼接 VO 返回，只返回 id、name、key
        return success(convertList(list, recipeMeal -> new RecipeMealRespVO()
                .setId(recipeMeal.getId()).setMealName(recipeMeal.getMealName()).setMealType(recipeMeal.getMealType())));
    }

}